module ActiveMerchant
  module Billing
    module AuthorizeNetCimAPI
      ID_CONDITION = [/^\d+$/, 'must be a number']
      
      class CreateCustomerProfile < Request
        self.composite_attributes << [ :profile, true, CustomerProfile]
      end
      
      class CreateCustomerPaymentProfile < Request
        self.scalar_attributes.concat [ [ :customerProfileId, true, /^\d+$/, 'must be a number'],
                                        [ :validationMode, true, [:testMode, :liveMode] ]
                                      ]

        self.composite_attributes << [ :paymentProfile, true, PaymentProfile]
      end
      
      class CreateCustomerShippingAddress < Request 
        self.scalar_attributes << [ :customerProfileId, true, /^\d+$/, 'must be a number']
        self.composite_attributes << [ :address, true, ShipToList]
      end
      
      class CreateCustomerProfileTransaction < Request
        self.composite_attributes << [:transaction, true, Transaction]
      end
      
      class DeleteCustomerProfile < Request
        self.scalar_attributes << [ :customerProfileId, true, /^\d+$/, 'must be a number']
      end
      
      class DeleteCustomerPaymentProfile < Request
        self.scalar_attributes.concat [ [ :customerProfileId, true, /^\d+$/, 'must be a number'],
                                        [ :customerPaymentProfileId, true, /^\d+$/, 'must be a number']
                                      ]
      end
      
      class DeleteCustomerShippingAddress < Request
        self.scalar_attributes.concat [ [ :customerProfileId, true, /^\d+$/, 'must be a number'],
                                        [ :customerAddressId, true, /^\d+$/, 'must be a number']
                                      ]  
      end
      
      class GetCustomerProfile < Request
        self.scalar_attributes = [ [ :customerProfileId, true, /^\d+$/, 'must be a number'] ]

        def parse_data(doc)
          CustomerProfile.new(doc[:profile])
        end
      end
      
      class GetCustomerPaymentProfile < Request
        self.scalar_attributes = [ [ :customerProfileId, true, /^\d+$/, 'must be a number'],
                                   [ :customerPaymentProfileId, true, /^\d+$/, 'must be a number']
                                 ]

        def parse_data(doc)
          PaymentProfile.new(doc[:paymentProfile])
        end
      end
      
      class GetCustomerShippingAddress < Request
        self.scalar_attributes = [ [ :customerProfileId, true, /^\d+$/, 'must be a number'],
                                   [ :customerAddressId, true, /^\d+$/, 'must be a number']
                                 ]
        def parse_data(doc)
          ShipToList.new(doc[:address])
        end                         
        
      end
      
      class UpdateCustomerProfile < Request
        self.composite_attributes << [ :profile, true, CustomerProfile]
      end

      class UpdateCustomerPaymentProfile < Request
        self.scalar_attributes << [ :customerProfileId, true, /^\d+$/, 'must be a number']
        self.composite_attributes << [ :paymentProfile, true, PaymentProfile]
      end

      class UpdateCustomerShippingAddress < Request
        self.scalar_attributes << [ :customerProfileId, true, /^\d+$/, 'must be a number']
        self.composite_attributes << [ :address, true, ShipToList]
      end

      class ValidateCustomerPaymentProfile < Request
        self.scalar_attributes = [ [ :customerProfileId, true, *ID_CONDITION],
                                   [ :customerPaymentProfileId, true, *ID_CONDITION],
                                   [ :customerAddressId, true, *ID_CONDITION],
                                   [ :validationMode, true, [:testMode, :liveMode]]
                                 ]
        def parse_data(doc)
          doc[:directResponse]
        end
      end
    end
  end
end